<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
        <el-table v-loading="loading" :data="list">
          <el-table-column label="用户编号" align="center" key="id" prop="id" />
          <el-table-column
            label="用户名称"
            align="center"
            prop="username"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="用户昵称"
            align="center"
            prop="nickname"
            :show-overflow-tooltip="true"
          />
          
          <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
          
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180"
          />
          <el-table-column label="操作" align="center" width="160">
            <template #default="scope">
              <div class="flex items-center justify-center">
                <el-button
                  type="primary"
                  link
                  @click="openRegisterForm(scope.row.id)"
                >
                  <Icon icon="ep:user" />为此用户挂号
                </el-button>
                
              </div>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>

  <ChildRegisterForm ref="formRef"/>
  <ChildUserForm ref="childFormRef" @success="getList"/>
</template>

<script lang="ts" setup>
import ChildRegisterForm from './ChildRegisterForm.vue'
import ChildUserForm from './ChildUserForm.vue'
import * as PatientApi from '@/api/medical/patient'
import { dateFormatter } from '@/utils/formatTime'
import { getUserProfile } from '@/api/system/user/profile'

const loading = ref(false)
const total = ref(0)
const list = ref([])
const formRef = ref()
const childFormRef = ref()

const queryParams = {
  pageNo: 1,
  pageSize: 10,
  id: undefined
}

const openForm = (type: string) => {
  childFormRef.value.open(type)
}

const openRegisterForm = (id: number) => {
  formRef.value.open(id)
}

const getList = async () => {
  loading.value = true
  const patients = await PatientApi.getChildrenPage(queryParams)
  list.value = patients.list
  total.value = patients.total
  loading.value = false
}

onMounted(async () => {
  await getList()
  getUserProfile().then((res) => {
    queryParams.id = res.id
    getList()
  })
})
</script>